package com.advert.presentation.tools;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.*;

import javax.faces.application.FacesMessage;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.util.*;


@Name("facesHelper")
@Scope(ScopeType.EVENT)
@AutoCreate
public class FacesHelper {

    @In(value = "redirectSurvivedMessages", scope = ScopeType.CONVERSATION, required = false)
    @Out(value = "redirectSurvivedMessages", scope = ScopeType.CONVERSATION, required = false)
    private Set<FacesMessage> redirectSurvivedMessages;


    public ExternalContext getExternalContext() {
        return getFacesContext().getExternalContext();
    }

    public ServletContext getServletContext() {
        return (ServletContext)getFacesContext().getExternalContext().getContext();
    }

    public HttpServletRequest getServletRequest() {
        return (HttpServletRequest)getExternalContext().getRequest();
    }

    public List<FacesMessage> getErrorMessages() {
        List<FacesMessage> retVal = new ArrayList<FacesMessage>();
        for (Iterator<FacesMessage> iterator = getFacesContext().getMessages(); iterator.hasNext(); ) {
            FacesMessage message = iterator.next();
            if (message.getSeverity() == FacesMessage.SEVERITY_ERROR) {
                retVal.add(message);
            }
        }
        for (Iterator<FacesMessage> iterator = getRedirectSurvivedMessages().iterator(); iterator.hasNext(); ) {
            FacesMessage message = iterator.next();
            if (message.getSeverity() == FacesMessage.SEVERITY_ERROR) {
                retVal.add(message);
                iterator.remove();
            }
        }
        return retVal;
    }

    public List<FacesMessage> getInfoMessages() {
        List<FacesMessage> retVal = new ArrayList<FacesMessage>();
        for (Iterator<FacesMessage> iterator = getFacesContext().getMessages(); iterator.hasNext(); ) {
            FacesMessage message = iterator.next();
            if (message.getSeverity() == FacesMessage.SEVERITY_INFO) {
                retVal.add(message);
            }
        }
        for (Iterator<FacesMessage> iterator = getRedirectSurvivedMessages().iterator(); iterator.hasNext(); ) {
            FacesMessage message = iterator.next();
            if (message.getSeverity() == FacesMessage.SEVERITY_INFO) {
                retVal.add(message);
                iterator.remove();
            }
        }
        return retVal;
    }

    public boolean hasErrors() {
        boolean retVal = false;
        for (Iterator<FacesMessage> iterator = getFacesContext().getMessages(); iterator.hasNext(); ) {
            FacesMessage message = iterator.next();
            if (message.getSeverity() == FacesMessage.SEVERITY_ERROR) {
                retVal = true;
                break;
            }
        }
        if (!retVal) {
            for (FacesMessage message : getRedirectSurvivedMessages()) {
                if (message.getSeverity() == FacesMessage.SEVERITY_ERROR) {
                    retVal = true;
                    break;
                }
            }
        }
        return retVal;
    }

    public boolean hasInfos() {
        boolean retVal = false;
        for (Iterator<FacesMessage> iterator = getFacesContext().getMessages(); iterator.hasNext(); ) {
            FacesMessage message = iterator.next();
            if (message.getSeverity() == FacesMessage.SEVERITY_INFO) {
                retVal = true;
                break;
            }
        }
        if (!retVal) {
            for (FacesMessage message : getRedirectSurvivedMessages()) {
                if (message.getSeverity() == FacesMessage.SEVERITY_INFO) {
                    retVal = true;
                    break;
                }
            }
        }
        return retVal;
    }

    public void addError(String message, boolean surviveRedirect) {
        FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR, message, null);
        if (surviveRedirect) {
            getRedirectSurvivedMessages().add(facesMessage);
        } else {
            getFacesContext().addMessage(null, facesMessage);
        }
    }

    public void addInfo(String message, boolean surviveRedirect) {
        FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_INFO, message, null);
        if (surviveRedirect) {
            getRedirectSurvivedMessages().add(facesMessage);
        } else {
            getFacesContext().addMessage(null, facesMessage);
        }
    }

    public FacesContext getFacesContext() {
        return FacesContext.getCurrentInstance();
    }


    private Set<FacesMessage> getRedirectSurvivedMessages() {
        if (redirectSurvivedMessages == null) {
            redirectSurvivedMessages = new HashSet<FacesMessage>();
        }
        return redirectSurvivedMessages;
    }

}
